package com.project.smarthome.base;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.sds.smarthome.foundation.util.XLog;

public abstract class BaseFragment extends Fragment implements View.OnClickListener, UIView {

    private ProgressDialog mLoadingDialog;
    protected View mView;

    protected View findViewById(int resourceId) {
        return mView.findViewById(resourceId);
    }

    protected abstract int getContentViewResource();

    protected abstract void initView(View view);

    protected abstract void initVariable();

    protected abstract void loadData();

    protected abstract void destroyView();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        XLog.d("onCreateView:" + this);
        mLoadingDialog = new ProgressDialog(getActivity());
        mLoadingDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        int viewId = getContentViewResource();
        mView = LayoutInflater.from(getActivity()).inflate(viewId, null);
        initView(mView);
        initVariable();
        loadData();
        return mView;
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onDestroyView() {
        XLog.d("onDestroyView:" + this);
        super.onDestroyView();
        destroyView();
        if (mLoadingDialog != null) {
            mLoadingDialog.dismiss();
        }
    }

    public void updateUI() {

    }

    public void showAdminDialog() {
//        SosDialog sosDialog = new SosDialog(getActivity());
//        sosDialog.getDialog(getActivity(), "请联系管理员", "确定");
    }
//
//    protected void initTitle(String title, int content) {
//        TextView txtTitle = (TextView) findViewById(R.id.txt_action_title);
//        txtTitle.setText(title);
//        ImageView imgLeft = (ImageView) findViewById(R.id.img_action_left);
//        imgLeft.setImageResource(content);
//        txtTitle.setOnClickListener(this);
//        imgLeft.setOnClickListener(this);
//    }
//
//    protected void initTitle(String title, String right) {
//        findViewById(R.id.img_action_left).setVisibility(View.GONE);
//        findViewById(R.id.img_action_right).setVisibility(View.GONE);
//
//        TextView txtTitle = (TextView) findViewById(R.id.txt_action_title);
//        txtTitle.setText(title);
//        TextView txtRight = (TextView) findViewById(R.id.txt_right);
//        txtRight.setVisibility(View.VISIBLE);
//        txtRight.setText(right);
//        txtRight.setOnClickListener(this);
//    }
//
//    protected void initTitle(String title, boolean isShow) {
//        TextView txtTitle = (TextView) findViewById(R.id.txt_action_title);
//        txtTitle.setText(title);
//        txtTitle.setOnClickListener(this);
//        if (isShow) {
//            findViewById(R.id.img_action_left).setVisibility(View.GONE);
//            findViewById(R.id.img_action_right).setVisibility(View.GONE);
//        } else {
//            findViewById(R.id.img_action_left).setOnClickListener(this);
//            findViewById(R.id.img_action_right).setOnClickListener(this);
//            findViewById(R.id.img_action_right).setVisibility(View.VISIBLE);
//        }
//    }
//
//    protected void initTitle(String title, int imgSource, int imgRight) {
//        TextView txtTitle = (TextView) findViewById(R.id.txt_action_title);
//        txtTitle.setText(title);
//        ImageView imgLeft = (ImageView) findViewById(R.id.img_action_left);
//        if (imgSource != -1) {
//            imgLeft.setImageResource(imgSource);
//        } else {
//            imgLeft.setVisibility(View.GONE);
//        }
//        findViewById(R.id.img_action_left).setOnClickListener(this);
//        ImageView imgActionRight = (ImageView) findViewById(R.id.img_action_right);
//        imgActionRight.setVisibility(View.VISIBLE);
//        imgActionRight.setImageResource(imgRight);
//        findViewById(R.id.txt_right).setVisibility(View.GONE);
//        imgActionRight.setOnClickListener(this);
//    }
//
//    protected void initIndexRight(int imgRight) {
//        TextView txtTitle = (TextView) findViewById(R.id.txt_action_title);
//        txtTitle.setVisibility(View.GONE);
//        ImageView imgLeft = (ImageView) findViewById(R.id.img_action_left);
//        imgLeft.setVisibility(View.GONE);
//        findViewById(R.id.img_action_left).setOnClickListener(this);
//        ImageView imgActionRight = (ImageView) findViewById(R.id.img_action_right);
//        imgActionRight.setVisibility(View.VISIBLE);
//        imgActionRight.setImageResource(imgRight);
//        findViewById(R.id.txt_right).setVisibility(View.GONE);
//        imgActionRight.setOnClickListener(this);
//    }
//
//    protected void initTitleRight(String title, int imgSource, String right) {
//        TextView txtTitle = (TextView) findViewById(R.id.txt_action_title);
//        txtTitle.setText(title);
//        ImageView imgLeft = (ImageView) findViewById(R.id.img_action_left);
//        imgLeft.setImageResource(imgSource);
//        findViewById(R.id.img_action_left).setOnClickListener(this);
//        TextView txtRight = (TextView) findViewById(R.id.txt_right);
//        txtRight.setText(right);
//        txtRight.setOnClickListener(this);
//        txtRight.setVisibility(View.VISIBLE);
//        ImageView imgActionRight = (ImageView) findViewById(R.id.img_action_right);
//        imgActionRight.setVisibility(View.GONE);
//    }
//
//    protected void initIndex(String right) {
//        TextView txtTitle = (TextView) findViewById(R.id.txt_action_title);
//        txtTitle.setVisibility(View.GONE);
//        ImageView imgLeft = (ImageView) findViewById(R.id.img_action_left);
//        imgLeft.setVisibility(View.GONE);
//        findViewById(R.id.img_action_left).setOnClickListener(this);
//        TextView txtRight = (TextView) findViewById(R.id.txt_right);
//        txtRight.setText(right);
//        txtRight.setOnClickListener(this);
//        txtRight.setVisibility(View.VISIBLE);
//        ImageView imgActionRight = (ImageView) findViewById(R.id.img_action_right);
//        imgActionRight.setVisibility(View.GONE);
//    }
//
//    public void startActivity(Context fromClass, Class<?> toClass) {
//        try {
//            Intent intent = new Intent();
//            intent.setClass(fromClass, toClass);
//            fromClass.startActivity(intent);
//        } catch (Exception e) {
//
//        }
//
//    }

    public void startActivity(Context fromClass, Class<?> toClass, Bundle bundle) {
        try {
            Intent intent = new Intent();
            intent.setClass(fromClass, toClass);
            if (null != bundle) {
                intent.putExtras(bundle);
            }
            fromClass.startActivity(intent);
        } catch (Exception e) {

        }

    }

    public void startActivityForResult(Context fromClass, Class<?> toClass, int requsetCode, Bundle bundle) {

        try {
            Intent intent = new Intent();
            intent.setClass(fromClass, toClass);
            if (null != bundle) {
                intent.putExtras(bundle);
            }
            startActivityForResult(intent, requsetCode);
        } catch (Exception e) {

        }
    }

    public void startActivityForResult(Context fromClass, Class<?> toClass, int requsetCode) {
        startActivityForResult(fromClass, toClass, requsetCode, null);
    }

    @Override
    public void onClick(View v) {

    }

    public void exit() {
        getActivity().finish();
    }

    public void showLoading(String msg) {
        mLoadingDialog.setMessage(msg);
        mLoadingDialog.setCanceledOnTouchOutside(true);
        if (mLoadingDialog.isShowing())
            return;
        mLoadingDialog.show();
    }

    public void showNoCanceledLoading(String msg) {
        mLoadingDialog.setMessage(msg);
        mLoadingDialog.setCanceledOnTouchOutside(false);
        if (mLoadingDialog.isShowing())
            return;
        mLoadingDialog.show();
    }

    protected void showToast(int resId) {
        Toast toast = Toast.makeText(getActivity(), getResources().getString(resId), Toast.LENGTH_SHORT);
        toast.show();
    }

    public void showToast(String msg) {
        Toast toast = Toast.makeText(getActivity(), msg, Toast.LENGTH_SHORT);
        toast.show();
    }

    public void hideLoading() {
        mLoadingDialog.dismiss();
    }

    @Override
    public void setMenuVisibility(boolean menuVisible) {
        super.setMenuVisibility(menuVisible);
        if (this.getView() != null)
            this.getView().setVisibility(menuVisible ? View.VISIBLE : View.GONE);
    }

    @Override
    protected void finalize() throws Throwable {
        super.finalize();
        XLog.i("gc for fragment:" + this);
    }

    @Override
    public void showLongToast(String msg) {

    }
}
